package cn.controller;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.service.sys.IOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.usualassessment.UserUATMzpdEntity;
import cn.kpi.entity.usualassessment.UserUATZpSummaryEntity;
import cn.kpi.service.usualassessment.IUserUATZfService;

@Controller
@RequestMapping("userusasstempzf")
public class UserUsAssTempZfController extends BaseController {
	@Autowired
	private IUserUATZfService iUserUATZfService;
	@Autowired
	private IOrgService iOrgService;


	@RequestMapping(value = "/user_usasstemp_zf_list")
	public String UserUsassTempZfList(Model model) {
		try{
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH )+1;
			int quarter=1;
			switch(month){
			case 1:
			case 2:
			case 3:
				quarter=1;
				break;
			case 4:
			case 5:
			case 6:
				quarter=2;
				break;
			case 7:
			case 8:
			case 9:
				quarter=3;
				break;
			case 10:
			case 11:
			case 12:
				quarter=4;
				break;
			}
			if(quarter==1){
				year=year-1;
				quarter=4;
			}
			else{
				quarter=quarter-1;
			}
			model.addAttribute("year", year);
			model.addAttribute("quarter", quarter);
		}
        catch(Exception ex){
			
		}
		return "usualassessment/user_usasstemp_zf_list";
	}
	
	//固化
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<UserUATMzpdEntity> GetList(HttpServletRequest request) {

		List<UserUATMzpdEntity> lst = null;
		LayTable<UserUATMzpdEntity> layTable = new LayTable<UserUATMzpdEntity>();
		try {
			String realname = request.getParameter("realname");
			int year = Integer.parseInt(request.getParameter("year"));
			int quarter = Integer.parseInt(request.getParameter("quarter"));
			String strorgid = request.getParameter("orgid");
			String where = "";
			String where2 ="";
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;

			where+=this.GetUserListByRole();

            if(strorgid!=null&&strorgid.length()>0){
//            	where+=" AND szdw="+strorgid;
				OrgEntity orgentity=iOrgService.GetOrgById(Long.parseLong(strorgid));
				where+=" AND  szdw IN (SELECT orgid FROM t_sys_org WHERE lft>="+orgentity.getLft()+" AND lft<="+orgentity.getRgt()+")";
            }
            else{
            	where+=" AND szdw IN ("+this.GetOrgListByRole()+")";
            }

			if(realname!=null&&realname.length()>0){
				where2+=" AND realname LIKE '%"+realname+"%'";
			}
			
			lst = iUserUATZfService.GetUserZfListProcess(where, where2,year, quarter, beginrow, endrow);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(page-1)*limit+(i + 1);
				lst.get(i).setRowNum(numr);

				String realorgName = lst.get(i).getRealorgname();
				if(realorgName ==null||realorgName.equals("")){
					lst.get(i).setRealorgname(lst.get(i).getOrgname());
				}
			}

			int iCount = iUserUATZfService.GetUserZfListProcessCount(where);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/user_pd_zf")
	public ExceResult UserPdZf(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long userid=Long.parseLong(request.getParameter("userid"));
		 int year=Integer.parseInt(request.getParameter("year"));
		 int quarter=Integer.parseInt(request.getParameter("quarter"));
		 
	
		 int iResult=iUserUATZfService.UserPjZf(userid, year, quarter);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
			ex.printStackTrace();
		}
		return result;
	}
}
